package com.lxk.ums.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.StrUtil;
import com.lxk.finance.domain.bo.AccUserAccountBo;
import com.lxk.finance.service.IAccUserAccountService;
import com.lxk.ums.domain.bo.UmsUserBo;
import com.lxk.ums.domain.vo.UmsUserVo;
import com.lxk.ums.service.IUmsUserService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户
 *
 * @author LXK-RY5
 * @date 2024-06-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ums/user")
public class UmsUserController extends BaseController {

    private final IUmsUserService umsUserService;
    private final IAccUserAccountService accUserAccountService;

    /**
     * 查询用户列表
     */
    @SaCheckPermission("ums:user:list")
    @GetMapping("/list")
    public TableDataInfo<UmsUserVo> list(UmsUserBo bo, PageQuery pageQuery) {
        return umsUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户列表
     */
    @SaCheckPermission("ums:user:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UmsUserBo bo, HttpServletResponse response) {
        List<UmsUserVo> list = umsUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户", UmsUserVo.class, response);
    }

    /**
     * 获取用户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("ums:user:query")
    @GetMapping("/{id}")
    public R<UmsUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        UmsUserVo umsUserVo = umsUserService.queryById(id);
        accUserAccountService.findByUserIdAndAccountType(id, AccUserAccountBo.AccountType.MONEY).ifPresent(item -> {
             umsUserVo.setBalance(item.getAmount());
         });
        accUserAccountService.findByUserIdAndAccountType(id, AccUserAccountBo.AccountType.POINT).ifPresent(item -> {
            umsUserVo.setIntegral(item.getAmount());
        });
        return R.ok(umsUserVo);
    }

    /**
     * 新增用户
     */
    @SaCheckPermission("ums:user:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UmsUserBo bo) {
        return toAjax(umsUserService.insertByBo(bo));
    }


    /**
     * 批量修改用户状态
     */
    @SaCheckPermission("ums:user:editStatus")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping("{userIdList}")
    public R<Void> editStatus(@PathVariable(value = "userIdList") String userIdList, @RequestBody UmsUserBo bo) {
        StrUtil.split(userIdList, ",").stream().parallel().forEach(userId -> {
            UmsUserBo update = new UmsUserBo();
            update.setId(Long.valueOf(userId));
            update.setStatus(bo.getStatus());
            umsUserService.updateByBo(update);
        });
        return toAjax(true);
    }

    /**
     * 修改用户
     */
    @SaCheckPermission("ums:user:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UmsUserBo bo) {
        return toAjax(umsUserService.updateByBo(bo));
    }

    /**
     * 删除用户
     *
     * @param ids 主键串
     */
    @SaCheckPermission("ums:user:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(umsUserService.deleteWithValidByIds(List.of(ids), true));
    }
}
